package top.yuyufeng.learn.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.MySqlMapper;
import tk.mybatis.mapper.common.SqlServerMapper;
import top.yuyufeng.learn.jdbc.MultipleDataSource;
import top.yuyufeng.learn.modal.GoodInfo;
import top.yuyufeng.learn.modal.UserInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuyufeng
 * @date 2018/5/17.
 */
@Component
@Aspect
public class MultipleDataSourceAspectAdvice {
    private static Map<String, String> classMap = new HashMap<>();
    static {
        classMap.put("top.yuyufeng.learn.dao.UserInfoDao", "myDataSource1");
        classMap.put("top.yuyufeng.learn.dao.GoodInfoDao", "myDataSource2");
    }
    @Around("execution(* top.yuyufeng.learn.dao.*.*(..))")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
        //根据Dao切换数据源
        String clazzName = jp.getTarget().getClass().getName();
        System.out.println(clazzName);
        MultipleDataSource.setDataSourceKey(classMap.get(clazzName));
        return jp.proceed();
    }
}
